using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Scene.Voice;
using System;
namespace MicroMsg.Plugin.DriftBottle.Scene
{
	public class ThrowVoiceBottleService
	{
		private const string TAG = "ThrowVoiceBottleService";
		private const int MAX_RUNNING = 5;
		private static ThrowVoiceBottleContext mWaitContext;
		private static TimerObject mTimerObject;
		public static bool doTextBottleScene(string string_0)
		{
			return new NetSceneThrowBottle().doTextBottleScene(string_0);
		}
		public static bool doVoiceBottleSceneBegin()
		{
			if (RecorderVoice.isRunning())
			{
				Log.smethod_1("ThrowVoiceBottleService", "scene cannot begin in recording. ");
				return false;
			}
			ThrowVoiceBottleContext recorderContext = new ThrowVoiceBottleContext();
			RecorderVoice.start(recorderContext, null);
			return true;
		}
		public static bool doVoiceBottleSceneEnd()
		{
			Log.smethod_5("ThrowVoiceBottleService", "doVoiceBottleSceneEnd begin");
			if (!UploadVoiceRecorder.isRunning())
			{
				Log.smethod_1("ThrowVoiceBottleService", "scene cannot end without begin, ignored. ");
				return false;
			}
			ThrowVoiceBottleContext throwVoiceBottleContext = (ThrowVoiceBottleContext)RecorderVoice.mRecorderContext;
			if (throwVoiceBottleContext == null)
			{
				Log.smethod_1("ThrowVoiceBottleService", "scene cannot end with null context, ignored. ");
				return true;
			}
			string hashString = MD5Core.GetHashString(string.Concat(Util.getNowMilliseconds()));
			throwVoiceBottleContext.mClientMsgID = hashString;
			Log.smethod_4("ThrowVoiceBottleService", "cmd to scene end,  clientMsgId = " + throwVoiceBottleContext.mClientMsgID);
			RecorderVoice.stop();
			if (throwVoiceBottleContext.isInvalidShortVoice())
			{
				throwVoiceBottleContext.mStatus = 4;
				Log.smethod_1("ThrowVoiceBottleService", "voice bottle scene end with invalid short voice. ");
				return false;
			}
			Log.smethod_5("ThrowVoiceBottleService", "doVoiceBottleSceneEnd end");
			return true;
		}
		public static void setWaitSendContext(ThrowVoiceBottleContext waitContext)
		{
			ThrowVoiceBottleService.mWaitContext = waitContext;
		}
		public static bool startDoSence()
		{
			Log.smethod_5("ThrowVoiceBottleService", "begin start service");
			if (ThrowVoiceBottleContextMgr.getInstance().countRunningContext() >= 5)
			{
				Log.smethod_1("ThrowVoiceBottleService", "send Image scene cannot begin ,running task too much. ");
				return false;
			}
			if (ThrowVoiceBottleService.mWaitContext == null)
			{
				return false;
			}
			ThrowVoiceBottleContextMgr.getInstance().putToHead(ThrowVoiceBottleService.mWaitContext);
			ThrowVoiceBottleService.checkReadyContextDispatcher();
			ThrowVoiceBottleService.mWaitContext = null;
			return true;
		}
		private static void checkReadyContextDispatcher()
		{
			if (ThrowVoiceBottleService.mTimerObject == null)
			{
				Log.smethod_5("ThrowVoiceBottleService", "upload image service dispatcher startup. ");
				ThrowVoiceBottleService.mTimerObject = TimerService.addTimer(8, new EventHandler(ThrowVoiceBottleService.onVoiceBottleContextDispatcher), 1, -1);
				ThrowVoiceBottleService.mTimerObject.start();
			}
		}
		public static void onVoiceBottleContextDispatcher(object sender, EventArgs e)
		{
			Log.smethod_5("ThrowVoiceBottleService", "start service complete!");
			ThrowVoiceBottleContextMgr.getInstance().clearnFinishedContext();
			if (ThrowVoiceBottleContextMgr.getInstance().countRunningContext() >= 5)
			{
				return;
			}
			ThrowVoiceBottleContext firstContextNeedHandle = ThrowVoiceBottleContextMgr.getInstance().getFirstContextNeedHandle();
			if (firstContextNeedHandle == null)
			{
				Log.smethod_4("ThrowVoiceBottleService", "No more voice bottle need send. ");
				ThrowVoiceBottleService.mTimerObject.stop();
				ThrowVoiceBottleService.mTimerObject = null;
				return;
			}
			NetSceneThrowBottle netSceneThrowBottle = new NetSceneThrowBottle();
			netSceneThrowBottle.doVoiceScene(firstContextNeedHandle);
		}
		private static void onSceneFinished(UploadVoiceContext context)
		{
		}
	}
}
